package cn.changgou.goods.service.impl;

import cn.changgou.goods.dao.BrandMapper;
import cn.changgou.goods.pojo.Brand;
import cn.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    /**
     * @Author ZhouNing
     * @Description : 查询所有品牌
     * @Date 2020/9/7
     * @return List<Brand>
     **/

    @Autowired
    private BrandMapper brandMapper;


    @Override
    public List<Brand> findALl() {
        return brandMapper.selectAll();
    }

    /**
     * @param id
     * @return cn.changgou.goods.pojo.Brand
     * @Author ZhouNing
     * @Description : 根据Id查询goods表
     * @Date 2020/9/7
     * @Param [id]
     */
    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * @param brand
     * @return void
     * @Author ZhouNing
     * @Description : 新增品牌
     * @Date 2020/9/7
     * @Param [brand]
     */
    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    /**
     * @param brand
     * @return void
     * @Author ZhouNing
     * @Description : 修改商品
     * @Date 2020/9/7
     * @Param [brand]
     */
    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * @param id
     * @return void
     * @Author ZhouNing
     * @Description :根据ID删除品牌
     * @Date 2020/9/7
     * @Param [id]
     */
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * @param searchMap
     * @return java.util.List<cn.changgou.goods.pojo.Brand>
     * @Author ZhouNing
     * @Description : 多条件搜索品牌方法
     * @Date 2020/9/7
     * @Param [searchMap]
     */
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        // 品牌名称
        if (searchMap != null && !"".equals(searchMap.get("name"))) {
            criteria.andEqualTo("name", searchMap.get("name"));
            //criteria.andLike("name","%"+searchMap.get("name")+"%");
        }
        // 品牌首字母
        if (searchMap != null && !"".equals(searchMap.get("letter"))) {

            criteria.andEqualTo("letter", searchMap.get("letter"));
        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    /**
     * @param page
     * @param size
     * @return com.github.pagehelper.Page<cn.changgou.goods.pojo.Brand>
     * @Author ZhouNing
     * @Description : 分页查询品牌
     * @Date 2020/9/7
     * @Param [page, size]
     */
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Brand>) brandMapper.selectAll();
    }

    /**
     * @param page
     * @param size
     * @param brand
     * @return com.github.pagehelper.Page<cn.changgou.goods.pojo.Brand>
     * @Author ZhouNing
     * @Description : 分页条件查询
     * @Date 2020/9/7
     * @Param [page, size, brand]
     */
    @Override
    public Page<Brand> findPage(int page, int size, Brand brand) {
        PageHelper.startPage(page, size);

        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        //品牌名查询
        if (brand != null) {
            criteria.andEqualTo("name", brand.getName());
        }
        if (brand != null) {
            criteria.andEqualTo("letter", brand.getLetter());
        }

        return (Page<Brand>) brandMapper.selectByExample(example);
    }
}
